<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue3.js"></script>
    <style>
        table{
            width: 80%;
            border: 2px solid black;
            border-collapse: collapse;
        }
        td,th{
            border: 1px solid black;
            line-height: 2em;
        }
    </style>
</head>
<body>
    <div id="app">
        <button>获取数据</button>
        <table>
            <thead>
                <th>ID</th><th>商品名称</th><th>库存</th>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    
    <script>
        let btn=document.querySelector("button")
        btn.addEventListener("click",()=>{
            let tbody=document.querySelector("table tbody")
            tbody.innerHTML=""
            fetch("http://localhost:3000/products")
            .then(resp=>resp.json())
            .then(prods=>{
                prods.forEach(prod => {
                    let tr=document.createElement("tr")
                    let td1=document.createElement("td")
                    let td2=document.createElement("td")
                    let td3=document.createElement("td")
                    td1.innerHTML=prod.id 
                    td2.innerHTML=prod.name
                    td3.innerHTML=prod.stock
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tbody.appendChild(tr)
                });
            })
        })
        
    </script>
</body>
</html>